package cn.zyy.hotel.controller.admin;

import cn.zyy.hotel.entity.Employee;
import cn.zyy.hotel.service.EmployeeService;
import cn.zyy.hotel.utils.Result;
import cn.zyy.hotel.utils.SystemConstant;
import cn.zyy.hotel.vo.EmployeeVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/employee")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    @Resource
    private RedisTemplate redisTemplate;

    //员工登录
    @RequestMapping("/login")
    public Object login(String username, String password, HttpSession session){
        HashMap<String, Object> map = new HashMap<String, Object>();
//        Employee employee = employeeService.login(username, password);
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            //保存当前登录用户
            Employee employee = employeeService.login(username);
            session.setAttribute(SystemConstant.LOGINUSER,employee);
            map.put(SystemConstant.SUCCESS,true);//成功
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            map.put(SystemConstant.SUCCESS,false);//失败
            map.put(SystemConstant.MESSAGE,"账号错误，登录失败");
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            map.put(SystemConstant.SUCCESS,false);//失败
            map.put(SystemConstant.MESSAGE,"密码错误，登录失败");
        }
        return map;
    }

    //员工列表
    @RequestMapping("/list")
    public Result list(EmployeeVO employeeVo){
        //设置分页信息
        PageHelper.startPage(employeeVo.getPage(),employeeVo.getLimit());
        //调用查询的方法
        List<Employee> employeeList = employeeService.selectEmployeeList(employeeVo);
        //创建分页对象
        PageInfo<Employee> pageInfo = new PageInfo<Employee>(employeeList);
        return Result.builder().code(0).count(pageInfo.getTotal()).data(pageInfo.getList()).build();
    }

    //添加员工
    @RequiresPermissions("employee:add")
    @RequestMapping("/addEmployee")
    public Object addEmployee(Employee employee, HttpSession session){
        Map<String,Object> map = new HashMap<String,Object>();
        //获取当前登录用户
        Employee loginUser = (Employee) session.getAttribute(SystemConstant.LOGINUSER);
        //设置创建人
        employee.setCreatedBy(loginUser.getId());
        //调用新增员工的方法
        if(employeeService.addEmployee(employee)>0) {
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, "添加成功");
            if (redisTemplate.hasKey(SystemConstant.LOGINUSER+employee.getLoginName())){
                redisTemplate.delete(SystemConstant.LOGINUSER+employee.getLoginName());
            }
        }else{
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, "添加失败");
        }
        return map;
    }

    //修改员工
    @RequiresPermissions("employee:edit")
    @RequestMapping("/updateEmployee")
    public Object updateEmployee(Employee employee, HttpSession session){
        Map<String,Object> map = new HashMap<String,Object>();
        //获取当前登录用户
        Employee loginUser = (Employee) session.getAttribute(SystemConstant.LOGINUSER);
        //设置修改人
        employee.setModifyBy(loginUser.getId());
        //调用新增员工的方法
        if(employeeService.updateEmployee(employee)>0) {
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, "修改成功");
            if (redisTemplate.hasKey(SystemConstant.LOGINUSER+employee.getLoginName())){
                redisTemplate.delete(SystemConstant.LOGINUSER+employee.getLoginName());
            }
        }else{
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, "修改失败");
        }
        return map;
    }

    //删除员工
    @RequiresPermissions("employee:delete")
    @RequestMapping("/deleteById")
    public Object deleteById(Integer id){
        Map<String,Object> map = new HashMap<>();
        Employee employee = employeeService.selectEmployeeById(id);
        //调用删除员工的方法
        if(employeeService.deleteById(id) > 0){
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, "删除成功");
            if (redisTemplate.hasKey(SystemConstant.LOGINUSER+employee.getLoginName())){
                redisTemplate.delete(SystemConstant.LOGINUSER+employee.getLoginName());
            }
        }else{
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, "删除失败");
        }
        return map;

    }

    //重置密码
    @RequiresPermissions("employee:reset")
    @RequestMapping("/resetPwd")
    public Object resetPwd(Integer id){
        Map<String,Object> map = new HashMap<String,Object>();
        Employee employee = employeeService.selectEmployeeById(id);
        //调用重置密码的方法
        if(employeeService.resetPwd(id)>0){
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, "密码重置成功");
            if (redisTemplate.hasKey(SystemConstant.LOGINUSER+employee.getLoginName())){
                redisTemplate.delete(SystemConstant.LOGINUSER+employee.getLoginName());
            }
        }else{
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, "密码重置失败");
        }
        return map;
    }

    //分配角色
    @RequiresPermissions("employee:auth")
    @RequestMapping("/saveEmployeeRole")
    public Object saveEmployeeRole(String roleIds,Integer empId){
        Map<String,Object> map = new HashMap<String,Object>();
        //调用保存员工角色关系的方法
        if(employeeService.saveEmployeeRole(roleIds,empId)){
            map.put(SystemConstant.MESSAGE,"角色分配成功");
        }else{
            map.put(SystemConstant.MESSAGE,"角色分配失败");
        }
        return map;
    }

    //修改密码
    @RequestMapping("/updatePwd")
    public Object updatePwd(EmployeeVO employeeVO,HttpSession session){
        Employee employee = (Employee)session.getAttribute(SystemConstant.LOGINUSER);
        employeeVO.setId(employee.getId());
        Map<String,Object> map = new HashMap<String,Object>();
        //调用重置密码的方法
        if(employeeService.updatePwd(employeeVO)>0){
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, "修改密码成功");
            session.removeAttribute(SystemConstant.LOGINUSER);
            if (redisTemplate.hasKey(SystemConstant.LOGINUSER+employee.getLoginName())){
                redisTemplate.delete(SystemConstant.LOGINUSER+employee.getLoginName());
            }
        }else{
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, "修改密码失败");
        }
        return map;
    }

}
